const net = require('net')
const MyTransform = require('./transform-my')

let overageBuffer = null
let ts = new MyTransform()

const client = net.createConnection({
  host: 'localhost',
  port: 1234,
})

client.write(ts.encode('荣光无限-1'))
client.write(ts.encode('荣光无限-2'))
client.write(ts.encode('荣光无限-3'))
client.write(ts.encode('荣光无限-4'))
client.write(ts.encode('荣光无限-5'))

client.on('data', (chunk) => {
  if (overageBuffer) {
    chunk = Buffer.concat([overageBuffer, chunk])
  }

  let packageLen = 0
  while ((packageLen = ts.getPackageLen(chunk))) {
    const packageContent = chunk.slice(0, packageLen)
    chunk = chunk.slice(packageLen)

    const ret = ts.decode(packageContent)
    console.info(ret)
  }
  overageBuffer = chunk
})
